// stores/alumni.js
import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
    createAlumni,
    getAlumnis,
    getAlumniDetail,
    getAlumniByStudentId,
    updateAlumni,
    deleteAlumni,
    batchCreateAlumni,
    getAlumniStatistics,
    verifyAlumni,
    getVerificationStatus
} from '@/api/alumni'

export const useAlumniStore = defineStore('alumni', () => {
    const alumniList = ref([])
    const currentAlumni = ref(null)
    const loading = ref(false)
    const hasMore = ref(true)
    const page = ref(1)
    const pageSize = ref(10)
    const statistics = ref(null)
    const verificationStatus = ref(null)
    const verifying = ref(false)

    // 获取校友列表
    const fetchAlumnisAction = async (params = {}) => {
        try {
            loading.value = true
            const response = await getAlumnis({
                page: page.value,
                pageSize: pageSize.value,
                ...params
            })

            if (response.code === 0) {
                if (params.page === 1 || !params.page) {
                    alumniList.value = response.data.list || response.data
                } else {
                    alumniList.value = [...alumniList.value, ...(response.data.list || response.data)]
                }

                // 检查是否还有更多数据
                const currentList = response.data.list || []
                hasMore.value = currentList.length === pageSize.value &&
                    response.data.pagination &&
                    response.data.pagination.total > alumniList.value.length

                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        } finally {
            loading.value = false
        }
    }

    // 加载更多校友
    const loadMoreAlumnisAction = async (params = {}) => {
        if (!hasMore.value || loading.value) return

        page.value += 1
        return await fetchAlumnisAction(params)
    }

    // 重置分页
    const resetPaginationAction = () => {
        page.value = 1
        hasMore.value = true
        alumniList.value = []
    }

    // 获取校友详情
    const fetchAlumniDetailAction = async (alumniId) => {
        try {
            const response = await getAlumniDetail(alumniId)
            if (response.code === 0) {
                currentAlumni.value = response.data
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 根据学号获取校友详情
    const fetchAlumniByStudentIdAction = async (studentId) => {
        try {
            const response = await getAlumniByStudentId(studentId)
            if (response.code === 0) {
                currentAlumni.value = response.data
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 创建校友
    const createAlumniAction = async (alumniData) => {
        try {
            const response = await createAlumni(alumniData)
            if (response.code === 0) {
                // 将新校友添加到列表开头
                alumniList.value.unshift(response.data)
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 更新校友信息
    const updateAlumniAction = async (alumniId, updateData) => {
        try {
            const response = await updateAlumni(alumniId, updateData)
            if (response.code === 0) {
                // 更新本地校友数据
                const index = alumniList.value.findIndex(alumni => alumni.id === alumniId)
                if (index !== -1) {
                    alumniList.value[index] = { ...alumniList.value[index], ...updateData }
                }

                if (currentAlumni.value && currentAlumni.value.id === alumniId) {
                    currentAlumni.value = { ...currentAlumni.value, ...updateData }
                }

                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 删除校友
    const deleteAlumniAction = async (alumniId) => {
        try {
            const response = await deleteAlumni(alumniId)
            if (response.code === 0) {
                // 从本地列表中移除
                alumniList.value = alumniList.value.filter(alumni => alumni.id !== alumniId)

                if (currentAlumni.value && currentAlumni.value.id === alumniId) {
                    currentAlumni.value = null
                }

                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 批量导入校友
    const batchCreateAlumniAction = async (alumniListData) => {
        try {
            const response = await batchCreateAlumni({ alumni_list: alumniListData })
            if (response.code === 0) {
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 获取统计信息
    const fetchAlumniStatisticsAction = async () => {
        try {
            const response = await getAlumniStatistics()
            if (response.code === 0) {
                statistics.value = response.data
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 搜索校友
    const searchAlumnisAction = async (searchParams = {}) => {
        try {
            loading.value = true
            const response = await getAlumnis({
                page: 1,
                pageSize: pageSize.value,
                ...searchParams
            })

            if (response.code === 0) {
                alumniList.value = response.data.list || response.data
                hasMore.value = false // 搜索状态下不显示加载更多
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        } finally {
            loading.value = false
        }
    }

    // 校友认证
    const verifyAlumniAction = async (verifyData) => {
        try {
            verifying.value = true
            const response = await verifyAlumni(verifyData)
            if (response.code === 0) {
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        } finally {
            verifying.value = false
        }
    }

    // 获取认证状态
    const fetchVerificationStatusAction = async (studentId) => {
        try {
            const response = await getVerificationStatus(studentId)
            if (response.code === 0) {
                verificationStatus.value = response.data
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 清除认证状态
    const clearVerificationStatus = () => {
        verificationStatus.value = null
    }

    return {
        alumniList,
        currentAlumni,
        loading,
        hasMore,
        page,
        pageSize,
        statistics,
        verificationStatus,
        verifying,
        fetchAlumnisAction,
        loadMoreAlumnisAction,
        resetPaginationAction,
        fetchAlumniDetailAction,
        fetchAlumniByStudentIdAction,
        createAlumniAction,
        updateAlumniAction,
        deleteAlumniAction,
        batchCreateAlumniAction,
        fetchAlumniStatisticsAction,
        searchAlumnisAction,
        verifyAlumniAction,
        fetchVerificationStatusAction,
        clearVerificationStatus
    }
})